﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using log4net;
using CashFlow.Db.Entity;
using SysAdmin._00_Def;
using SysAdmin._03_Service._00_Db;
using SysAdmin._04_Form._00_Common;

namespace SysAdmin._04_Form._01_Shop
{
    public partial class AddShopUserForm : Form
    {
        private static ILog _logger = LogManager.GetLogger(typeof(AddShopUserForm));
        private IList<UserInfoAndReferenceUserInfo> UserList { get; set; }
        public int ShopManagerUserId { get; set; }

        public AddShopUserForm()
        {
            InitializeComponent();
        }

        private void AddShopUserForm_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
        }

        private void AddShopUserForm_Shown(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                UserList = UserService.QueryByWhere2(textBox1.Text.Trim()
                            , textBox2.Text.Trim());
                var ls = UserList.Select(x => new DataGridItemData
                {
                    Id = x.UserInfo.Id,
                    Phone = x.UserInfo.Phone,
                    Name = x.UserInfo.Name,
                    InvitationCode = x.UserInfo.InvitationCode,
                    Reference = x.ReferenceUserInfo.Name,
                    ReferenceInvitationCode = x.UserInfo.ReferenceInvitationCode,
                    CreateTime = x.UserInfo.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")
                }).ToList();
                dataGridView1.DataSource = ls;
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                UICommonTool.ShowAlertDialog(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (e.ColumnIndex == 1)
                {
                    if ((bool)dataGridView1.Rows[e.RowIndex].Cells[1].FormattedValue == true)
                    {
                        dataGridView1.Rows[e.RowIndex].Cells[1].Value = false;
                    }
                    else
                    {
                        dataGridView1.Rows[e.RowIndex].Cells[1].Value = true;
                    }
                }
            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            var ls = new List<int>();
            foreach (DataGridViewRow item in dataGridView1.Rows)
            {
                if ((bool)item.Cells[1].FormattedValue == true)
                {
                    int userId = (int)item.Cells[0].Value;
                    ls.Add(userId);
                }
            }

            if (ls.Count() == 1)
            {
                try
                {
                    bool b = ShopUserService.Add(ShopManagerUserId, ls.ElementAt(0));
                    if (b)
                    {
                        UICommonTool.ShowInfoDialog("添加店铺成员成功！");
                        DialogResult = DialogResult.OK;
                        Close();
                    }
                    else
                    {
                        UICommonTool.ShowAlertDialog("添加店铺成员失败！");
                    }
                }
                catch (MyException ex)
                {
                    UICommonTool.ShowAlertDialog(ex.Message);
                }
                catch (Exception ex)
                {
                    _logger.Error(ex);
                    UICommonTool.ShowAlertDialog(ex.Message);
                }
            }
            else
            {
                UICommonTool.ShowAlertDialog("只能选择一个");
            }
        }

        private class DataGridItemData
        {
            public int Id { get; set; }
            public string Phone { get; set; }
            public string Name { get; set; }
            public string InvitationCode { get; set; }
            public string Reference { get; set; }
            public string ReferenceInvitationCode { get; set; }
            public string CreateTime { get; set; }
        }
    }
}
